package com.ita.servlet.action;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ita.dao.GoodsDao;
import com.ita.dao.impl.GoodsDaoImpl;
import com.ita.pojo.Goods;

public class UpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private GoodsDao goodsDao;

	public UpdateServlet() {

		goodsDao = new GoodsDaoImpl();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String pric = request.getParameter("price");
		String nu = request.getParameter("num");
		String date = request.getParameter("pdate");
		String wids = request.getParameter("wid");

		Date pdate = null;
		double price = Double.parseDouble(pric);
		Integer num = Integer.parseInt(nu);
		Integer wid = Integer.parseInt(wids);
		SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			pdate = sdFormat.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		Goods goods = new Goods(id, name, price, num, pdate, wid);

		goodsDao.updateGoods(goods);
		response.sendRedirect("show?wid=" + wid);

	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
